#include "stdafx.h"
#include "GdiRaster.h"

VOID GdiRasterReset(GdiRaster * raster, HDC hdc)
{
	raster->base.move_to = GdiRasterMoveTo;
	raster->base.line_to = GdiRasterLineTo;
	raster->base.curve_to = GdiRasterCurveTo;
	raster->base.line_close = GdiRasterLineClose;
	raster->base.curve_close = GdiRasterCurveClose;
	raster->hdc = hdc;

	raster->x_off = raster->y_off = 0.0f;
	raster->x_rate = raster->y_rate = 1.0f;
}

void GdiRasterMoveTo(tag_glyf_raster * raster, float_32 x, float_32 y)
{
	GdiRaster * ras = (GdiRaster *)raster;
	ras->x_first = x;
	ras->y_first = y;
	x *= ras->x_rate;
	y *= ras->y_rate;
	x += ras->x_off;
	y += ras->y_off;
	ras->x = x;
	ras->y = y;
	MoveToEx(ras->hdc, (int_32)x, (int_32)y, NULL);
	printf("MoveTo : (%5.2f, %5.2f)\n", x, y);
}
void GdiRasterLineTo(tag_glyf_raster * raster, float_32 x, float_32 y)
{
	GdiRaster * ras = (GdiRaster *)raster;
	x *= ras->x_rate;
	y *= ras->y_rate;
	x += ras->x_off;
	y += ras->y_off;
	ras->x = x;
	ras->y = y;
	LineTo(ras->hdc, (int_32)x, (int_32)y);
	printf("LineTo : (%5.2f, %5.2f)\n", x, y);
}
void GdiRasterCurveTo(tag_glyf_raster * raster, float_32 x2, float_32 y2, float_32 x3, float_32 y3)
{
	GdiRaster * ras = (GdiRaster *)raster;
	x2 *= ras->x_rate;
	y2 *= ras->y_rate;
	x3 *= ras->x_rate;
	y3 *= ras->y_rate;
	x2 += ras->x_off;
	y2 += ras->y_off;
	x3 += ras->x_off;
	y3 += ras->y_off;

	POINT pts[3] =  
	{
		{int_32(ras->x + 2 * x2) / 3, int_32(ras->y + 2 * y2) / 3},
		{int_32(2 * x2 + x3) / 3, int_32(2 * y2 + y3) / 3},
		{int_32(x3), int_32(y3)}
	};
	ras->x = x3;
	ras->y = y3;
	PolyBezierTo(ras->hdc, pts, 3);
	printf("CurveTo : (%5.2f, %5.2f), (%5.2f, %5.2f)\n", x2, y2, x3, y3);
}

void GdiRasterLineClose(tag_glyf_raster * raster)
{
	GdiRaster * ras = (GdiRaster *)raster;
	GdiRasterLineTo(raster, ras->x_first, ras->y_first);
}
void GdiRasterCurveClose(tag_glyf_raster * raster, float_32 x2, float_32 y2)
{
	GdiRaster * ras = (GdiRaster *)raster;
	GdiRasterCurveTo(raster, x2, y2, ras->x_first, ras->y_first);
}
